from app.extensions import db
from app.models import Role, Permission, ModuleType, OperationType, User, UserProfile

def init_roles():
    roles = [
        {'name': '超级管理员', 'code': 'super_admin', 'description': '超级管理员具有所有的权限'},
    ]
    for role_data in roles:
        if not Role.query.filter_by(code=role_data['code']).first():
            role = Role(**role_data)
            db.session.add(role)
    db.session.commit()

def init_permissions():
    permissions = [
        # 用户管理权限
        {'module': ModuleType.ADMIN_USER, 'operation': OperationType.QUERY, 'description': '查询用户'},
        {'module': ModuleType.ADMIN_USER, 'operation': OperationType.ADD, 'description': '新增用户'},
        {'module': ModuleType.ADMIN_USER, 'operation': OperationType.UPDATE, 'description': '修改用户'},
        {'module': ModuleType.ADMIN_USER, 'operation': OperationType.DELETE, 'description': '删除用户'},
        {'module': ModuleType.ADMIN_USER, 'operation': OperationType.RESET_PASSWORD, 'description': '重置密码'},
    
        # 角色管理的完整CRUD权限
        {'module': ModuleType.ADMIN_ROLE, 'operation': OperationType.QUERY, 'description': '查询权限'},
        {'module': ModuleType.ADMIN_ROLE, 'operation': OperationType.ADD, 'description': '新增权限'},
        {'module': ModuleType.ADMIN_ROLE, 'operation': OperationType.UPDATE, 'description': '修改权限'},
        {'module': ModuleType.ADMIN_ROLE, 'operation': OperationType.DELETE, 'description': '删除权限'},
        
        # 日志
        {'module': ModuleType.ADMIN_LOG, 'operation': OperationType.QUERY, 'description': '查询日志'},
    ]
    
    for perm_data in permissions:
        if not Permission.query.filter_by(module=perm_data['module'], 
                                        operation=perm_data['operation']).first():
            permission = Permission(**perm_data)
            db.session.add(permission)
    db.session.commit()
    
def init_user():
    users = [
        {
            'email': 'admin@163.com','password':'scrypt:32768:8:1$hmBCT3sz744QHuuo$d7fbd99f70152353ef52f6fcff1cb1adada6f8080cb9aacf9e42098d85508866b74bbcfb2d3f5a8cce28c668b2eaa06842e237bfef2c8522f86b4684dde869e2','is_active':True,'role_id':1
        }
    ]
    
    for user_data in users:
        if not User.query.filter_by(email=user_data['email']).first():
            user = User(**user_data)
            db.session.add(user)
    db.session.commit()


def init_user_profile():
    user_profiles = [
        {
            'user_id': 1, 'name': 'root', 'phone': '', 'gender': '', 'birthday': '2025-01-01', 'address': ''
        }
    ]
    
    for user_profile_data in user_profiles:
        if not UserProfile.query.filter_by(user_id=user_profile_data['user_id']).first():
            user_profile = UserProfile(**user_profile_data)
            db.session.add(user_profile)
    db.session.commit()
    
def init_role_permissions():
    admin_role = Role.query.filter_by(code='super_admin').first()
    if not admin_role:
        return
    
    all_permissions = Permission.query.all()
    
    for permission in all_permissions:
        if permission not in admin_role.permissions:
            admin_role.permissions.append(permission)
    
    db.session.commit()

def init_data():
    init_roles()
    init_permissions()
    init_user()
    init_user_profile()
    init_role_permissions()


    
